import { message } from 'antd';
import fetch from '../utils/request';

const queryActivityList = ({ payload }) => fetch.post('/haierp1/activity/queryActivityList', { data: payload }).catch(e => e);
const queryActivity = ({ payload }) => fetch.post('/haierp1/activity/query', { data: payload }).catch(e => e);
const addActivity = ({ payload }) => fetch.post('/haierp1/activity/add', { data: payload }).catch(e => e);
const updateActivity = ({ payload }) => fetch.post('/haierp1/activity/update', { data: payload }).catch(e => e);
const deleteActivity = ({ payload }) => fetch.post('/haierp1/activity/delete', { data: payload }).catch(e => e);
const getDimensioncodePic = ({ payload }) => fetch.post('/haierp1/activity/createActivityItemDimensioncode', { data: payload }).catch(e => e);
export default {
  namespace: 'activity',
  state: {
    activityList: [],
    activityPage: 1,
    activityPageSize: 20,
    activityTotal: 1,
    activityValues: {},
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/person/activityManageList' && !window.existCacheState('/person/activityManageList')) {
          setTimeout(() => {
            dispatch({ type: 'queryActivityList', payload: {} });
          }, 0);
        }
      });
    },
  },

  effects: {
    * queryActivityList({ payload }, { call, put, select }) { // SKU管理列表
      let pageIndex = yield select(({ activity }) => activity.activityPage);
      let pageSize = yield select(({ activity }) => activity.activityPageSize);
      if (payload.pageIndex) {
      	pageIndex = payload.pageIndex;
        yield put({ type: 'saveActivityPage', payload });
      }
      if (payload.pageSize) {
        pageSize = payload.pageSize;
        yield put({ type: 'saveActivityPageSize', payload });
      }
      const data = yield call(queryActivityList, { payload: { ...payload, pageIndex, pageSize } });
      if (data.success) {
        yield put({ type: 'updateActivityList', payload: data, });
      }
    },
    * queryActivity({ payload }, { call, put }) {
      const newPayload = { ...payload };
      const data = yield call(queryActivity, { payload: newPayload });
      if (data.success) {
        yield put({
          type: 'saveActivity',
          payload: data,
        });
      }
    },
    * addActivity({ payload }, { call }) {
      const data = yield call(addActivity, { payload });
      if (data.success) {
        message.success('增加商品活动成功');
      }
    },
    * updateActivity({ payload }, { call }) {
      const data = yield call(updateActivity, { payload });
      if (data.success) {
        message.success('更新商品活动成功');
      }
    },
    * deleteActivity({ payload, cb }, { call }) {
      const data = yield call(deleteActivity, { payload });
      if (data.success) {
        message.success('删除商品活动成功');
        cb();
      }
    },
    * getDimensioncode({ payload, cb }, { call }) { // 二维码生成成功
      const data = yield call(getDimensioncodePic, { payload });
      if (data.success) {
        cb(data);
      }
    },
  },

  reducers: {
    saveActivityPage(state, { payload }) {
      return { ...state, activityPage: payload.pageIndex };
    },
    saveActivityPageSize(state, { payload }) {
      return { ...state, activityPageSize: payload.pageSize };
    },
    updateActivityList(state, { payload }) {
      return { ...state, activityList: payload.data, activityTotal: payload.totalCount };
    },
    saveActivity(state, { payload }) {
      return { ...state, activityValues: payload };
    },
  },
};
